using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NorthwindWeb.Models;

namespace NorthwindWeb.Controllers
{   
    public class OrderController : Controller
    {
		private readonly ICustomerRepository customerRepository;
		private readonly IEmployeeRepository employeeRepository;
		private readonly IOrderRepository orderRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public OrderController() : this(new CustomerRepository(), new EmployeeRepository(), new OrderRepository())
        {
        }

        public OrderController(ICustomerRepository customerRepository, IEmployeeRepository employeeRepository, IOrderRepository orderRepository)
        {
			this.customerRepository = customerRepository;
			this.employeeRepository = employeeRepository;
			this.orderRepository = orderRepository;
        }

        //
        // GET: /Order/

        public ViewResult Index()
        {
            ViewBag.PossibleCustomers = customerRepository.All;
            ViewBag.PossibleEmployees = employeeRepository.All;
            return View(orderRepository.AllIncluding(order => order.Customer, order => order.Employee));
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "OrderID")]Order order)
        {
            if (ModelState.IsValid) {
                orderRepository.InsertOrUpdate(order);
                orderRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCustomers = customerRepository.All;
				ViewBag.PossibleEmployees = employeeRepository.All;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid) {
                orderRepository.InsertOrUpdate(order);
                orderRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleCustomers = customerRepository.All;
				ViewBag.PossibleEmployees = employeeRepository.All;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Order/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            orderRepository.Delete(id);
            orderRepository.Save();

            return RedirectToAction("Index");
        }

        
        public JsonResult orderGridData(int rows, int page)
        {
        	var count = orderRepository.All.Count();
            var pageData = orderRepository.All.OrderBy(x => x.OrderID).Skip((page - 1) * rows).Take(rows).Select(o => new {OrderID = o.OrderID, CustomerID = o.CustomerID, EmployeeID = o.EmployeeID, OrderDate = o.OrderDate, RequiredDate = o.RequiredDate, ShippedDate = o.ShippedDate, ShipVia = o.ShipVia, Freight = o.Freight, ShipName = o.ShipName, ShipAddress = o.ShipAddress, ShipCity = o.ShipCity, ShipRegion = o.ShipRegion, ShipPostalCode = o.ShipPostalCode, ShipCountry = o.ShipCountry});
            return Json(new { 
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
            }, JsonRequestBehavior.AllowGet);
        }
        
        
    }
}

